package com.ptswitch.material.pages.material;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.ActivationRequestParameter;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.SplitPageInfo;
import org.slf4j.Logger;

import com.ptswitch.material.model.material.Material;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.MaterialService;
import com.ptswitch.material.util.MaterialUtil;
import com.ptswitch.material.util.security.PermissionUtil;

public class LikeMaterial extends PageBase {
    @Inject
    private Logger logger;
    @Inject
    private MaterialService materialSer;
    @Property
    private SplitPageInfo page = new SplitPageInfo();
    //
    @Property
    private Material material;
    @Property
    private List<Material> list;
    //
    @ActivationRequestParameter
    private String c;//code
    @ActivationRequestParameter
    private String n;//name;
    @ActivationRequestParameter
    private String sp;//spce
    @ActivationRequestParameter
    private String d;//desc
    @ActivationRequestParameter
    private String p;//pcb
    @ActivationRequestParameter
    private String st;//state
    @ActivationRequestParameter
    private String f;//foot
    @ActivationRequestParameter
    private String br;//brand
    @ActivationRequestParameter
    private String su;//supplier
    @ActivationRequestParameter
    private String re;//remark

    //

    // init()->initalize->onPassivate->init(xxx)->display
    public Object withInit(Material material, long size, long first) {
        page.setPageSize(size);
        page.setCurrentPage(first);
        //
        c = material.getCode();
        n = material.getName();
        sp = material.getSpecification();
        d = material.getDescription();
        p = material.getPcbDecal();
        st = material.getState();
        f = material.getFootprint();
        br = material.getBrand();
        su = material.getSupplier();
        re = material.getRemark();
        return this;
    }

    @OnEvent(value = EventConstants.ACTIVATE)
    void init() {
        checkPermitted(PermissionUtil.MaterialList);
    }

    @OnEvent(value = EventConstants.ACTIVATE)
    void init(long size, long first) {
        checkPermitted(PermissionUtil.MaterialList);
        this.material = MaterialUtil.toMaterial(c, n, sp, d, p, st, f, br, su, re);
        page.setPageSize(size);
        page.setCurrentPage(first);
    }

    @SetupRender
    Object display() {
        if (material != null) {
            page.setRowCount(materialSer.countLikeByExample(material));
            list = materialSer.findLikeByExample(material, page.getCurrentRow(), page.getPageSize());
        }
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return new Object[] { page.getPageSize(), page.getCurrentPage() };
    }
}
